package com.example.iqiyitest.activity;

import java.io.Serializable;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.example.iqiyitest.MainActivity;
import com.example.iqiyitest.R;
import com.example.iqiyitest.R.id;
import com.example.iqiyitest.adapter.DaohangAdapter;
import com.example.iqiyitest.adapter.MovieListAdapter;
import com.example.iqiyitest.adapter.DaohangAdapter.OnMenuClickListener;
import com.example.iqiyitest.info.FenleiInfo;
import com.example.iqiyitest.info.MovieDetailInfo;
import com.example.iqiyitest.adapter.MyMovieAdapter;
import com.example.iqiyitest.base.Constants;
import com.iqiyi.movieksdk.sdk.exported.HttpResult;
import com.iqiyi.movieksdk.sdk.exported.IMsgReceiver;
import com.iqiyi.movieksdk.sdk.exported.MovieSdk;
import com.iqiyi.movieksdk.sdk.exported.OnResponseListener;
import com.iqiyi.movieksdk.sdk.exported.OrderBy;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class DetailActivity extends Activity implements IMsgReceiver {
	ListView daohangList;
	GridView leixingGrid, chandiGrid, shijianGrid, movieGrid;
	LinearLayout shaixuanly;
	DaohangAdapter daohangAdapter, leixingAdapter, chandiAdapter, shijianAdapter;
	MovieListAdapter movieAdapter;
	ArrayList<String> daohangInfos;
	private OnMenuClickListener menuClickListener;
	String TAG = "iqiyiTest";
	private String host = "192.168.1.241";// 正式
	private long testQipuId = 723849600;// 仙球大战
	private MovieSdk movieSdk;
	ArrayList<FenleiInfo> fenleiInfos;
	ArrayList<MovieDetailInfo> movieInfos;
	String fenlei_get="0";
	String fenlei="全部",diqu="全部",shijian="	全部";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_detail);
		 Intent intent;
	        if(getIntent()!=null) {
	        	intent=getIntent();
	        	fenlei_get=intent.getStringExtra("from");
	        	Log.e("iqiyitest",fenlei_get);
	        	switch (fenlei_get) {
				case "0":
					fenlei="全部";
					diqu="全部";
					shijian="全部";
					break;
				case "1":
					fenlei="全部";
					diqu="全部";
					shijian="2017";
					break;
				case "2":
					fenlei="全部";
					diqu="美国";
					shijian="全部";
					break;
				case "3":
					fenlei="全部";
					diqu="内地";
					shijian="全部";
					break;
				case "4":
					fenlei="全部";
					diqu="美国";
					shijian="全部";
					break;

				default:
					break;
				}
	        }
		movieSdk = MovieSdk.init(getApplication(), Constants.serviceUrl, new MovieSdk.OnInitListener() {
			@Override
			public void initOver(int code) {
				Toast.makeText(DetailActivity.this, "init code:" + code, Toast.LENGTH_SHORT).show();
			}
		});
		movieSdk.addMsgReceiver(this);
		// text = (TextView) findViewById(R.id.tv);
		// image = (ImageView) findViewById(R.id.iv);
		movieSdk.sdk_movieLabel(new OnResponseListener() {
			@Override
			public void onResponse(HttpResult result) {
				Log.d(TAG, "分类" + result.data);
				// text.setText(result.data);
				fenleiInfos=getFenleiList(result.data);
				Log.d(TAG, "分类111111111111111" + fenleiInfos.toString());
			}
		});
		//筛选
		movieSdk.sdk_listMovieByLabel(fenlei, diqu, shijian, OrderBy.ORDER_XIAZAI, false, 1, 10, new OnResponseListener() {
			@Override
			public void onResponse(HttpResult result) {
				Log.d(TAG, "全部:" + result.data);
				// text.setText(result.data);
				movieInfos=getQuanbuList(result.data);
			}
		});
		daohangList = (ListView) findViewById(R.id.listView1);
		shaixuanly = (LinearLayout) findViewById(R.id.shaixuanly);
		leixingGrid = (GridView) findViewById(R.id.grid1);
		chandiGrid = (GridView) findViewById(R.id.grid2);
		shijianGrid = (GridView) findViewById(R.id.grid3);
		movieGrid=(GridView) findViewById(R.id.gridView1);
		init();
	}

	public void init() {
		daohangInfos = new ArrayList<String>();
		daohangInfos.add("筛选");
		daohangInfos.add("新片速递");
		daohangInfos.add("热映大片");
		daohangInfos.add("华语排行");
		daohangInfos.add("外语排行");
		daohangInfos.add("爆笑喜剧");
		daohangInfos.add("动作科幻");
		daohangInfos.add("家庭伦理");
		menuClickListener = new OnMenuClickListener() {

			@Override
			public void onClick(int position, View v) {
				String movieDetailInfo = daohangInfos.get(position);
				Log.e(TAG, movieDetailInfo.toString());
			}
		};
	
		daohangAdapter = new DaohangAdapter(DetailActivity.this, R.layout.item_listview, daohangInfos,
				menuClickListener);
		daohangList.setAdapter(daohangAdapter);
//		leixingAdapter=new DaohangAdapter(DetailActivity.this, R.layout.item_listview, fenleiInfos.get(0).getData(),
//				menuClickListener);
//		leixingGrid.setAdapter(leixingAdapter);
//		chandiAdapter=new DaohangAdapter(DetailActivity.this, R.layout.item_listview, fenleiInfos.get(1).getData(),
//				menuClickListener);
//		chandiGrid.setAdapter(chandiAdapter);
//		shijianAdapter=new DaohangAdapter(DetailActivity.this, R.layout.item_listview, fenleiInfos.get(2).getData(),
//				menuClickListener);
//		shijianGrid.setAdapter(shijianAdapter);
	}

	@Override
	public void onReceived(final String s) {
		Toast.makeText(this, "msg:" + s, Toast.LENGTH_SHORT).show();
		Log.d(TAG, "msg" + s);
		// text.post(new Runnable() {
		// @Override
		// public void run() {
		// text.setText("收到消息："+s);
		// }
		// });

	}
	public ArrayList<FenleiInfo> getFenleiList(String data) {
		JSONObject jsonObject;
		FenleiInfo fenleiInfo;
		ArrayList<String> datas;
		String data_item;
		ArrayList<FenleiInfo> fenleiInfos_old;
		fenleiInfos_old=new ArrayList<FenleiInfo>();
		try {
			jsonObject = new JSONObject(data);
			JSONArray jsonArray=jsonObject.getJSONArray("cate");
			for(int i=0;i<jsonArray.length();i++) {
				 jsonObject = jsonArray.getJSONObject(i);
				 fenleiInfo=new FenleiInfo();
				 fenleiInfo.setType(jsonObject.optString("type"));
				 fenleiInfo.setName(jsonObject.optString("name"));
				 JSONArray jsonArray2=jsonObject.getJSONArray("data");
				 datas=new ArrayList<String>();
				 for(int j=0;j<jsonArray2.length();j++) {
					 data_item =jsonArray2.get(j).toString();
					 datas.add(data_item);
				 }
				 fenleiInfo.setData(datas);
				 fenleiInfos_old.add(fenleiInfo);
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		new Thread() {public void run() {
			 Log.e(TAG, "1111222222222222222222222222222");
			Message m = new Message();
			m.what=1;
			splashHandler.sendMessage(m);
			
		   }}.start();
		   return fenleiInfos_old;
	}
	public ArrayList<MovieDetailInfo> getQuanbuList(String data) {
		JSONObject jsonObject;
		MovieDetailInfo movieDetailInfo;
		String data_item;
		ArrayList<MovieDetailInfo> movieInfos_old;
		movieInfos_old=new ArrayList<MovieDetailInfo>();
		try {
			jsonObject = new JSONObject(data);
			JSONArray jsonArray=jsonObject.getJSONArray("result");
			for(int i=0;i<jsonArray.length();i++) {
				 jsonObject = jsonArray.getJSONObject(i);
				 movieDetailInfo=new MovieDetailInfo();
				 movieDetailInfo.setPosterLocalImg(jsonObject.optString("posterLocalImg"));
				 movieDetailInfo.setDuration(jsonObject.optInt("duration"));
				 movieDetailInfo.setShotDesc(jsonObject.optString("shotDesc"));
				 movieDetailInfo.setDescription(jsonObject.optString("description"));
				 movieDetailInfo.setScore(jsonObject.optInt("score"));
				 movieDetailInfo.setShowTime(jsonObject.optInt("showtime"));
				 movieDetailInfo.setPlayType(jsonObject.optString("playType"));
				 movieDetailInfo.setPlayTimes(jsonObject.optInt("playTimes"));
				 movieDetailInfo.setActor(jsonObject.optString("actor"));
				 movieDetailInfo.setQipuId(jsonObject.optInt("qipuId"));
				 movieDetailInfo.setDirector(jsonObject.optString("director"));
				 movieDetailInfo.setDisplayName(jsonObject.optString("displayName"));
				 movieDetailInfo.setVideoType(jsonObject.optString("videoType"));
				 movieInfos_old.add(movieDetailInfo);
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		new Thread() {public void run() {
			 Log.e(TAG, "3333333333333333333333");
			Message m = new Message();
			m.what=2;
			splashHandler.sendMessage(m);
			
		   }}.start();
		   return movieInfos_old;
	}
	private Handler splashHandler = new Handler() {
	     public void handleMessage(Message msg) {
	       switch (msg.what) {
	       case 1:
	    	   Log.e(TAG, "11111111111111111111111111,"+fenleiInfos.get(0).getData());
	    	leixingAdapter=new DaohangAdapter(DetailActivity.this, R.layout.item_listview, fenleiInfos.get(0).getData(),
	   				menuClickListener);
	   		leixingGrid.setAdapter(leixingAdapter);
	   		chandiAdapter=new DaohangAdapter(DetailActivity.this, R.layout.item_listview, fenleiInfos.get(1).getData(),
	   				menuClickListener);
	   		chandiGrid.setAdapter(chandiAdapter);
	   		shijianAdapter=new DaohangAdapter(DetailActivity.this, R.layout.item_listview, fenleiInfos.get(2).getData(),
	   				menuClickListener);
	   		shijianGrid.setAdapter(shijianAdapter);
	        break;
	       case 2:
	    	   Log.e(TAG, "444444444444444444444444,"+movieInfos.toString());
	    	   movieAdapter=new MovieListAdapter(DetailActivity.this, R.layout.item_second_activity, movieInfos);
	    	   movieGrid.setAdapter(movieAdapter);
//	    	   movieGrid.setOnItemClickListener(new OnItemClickListener() {
//				
//				@Override
//				public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//					// TODO Auto-generated method stub
//					Intent intent=new Intent(DetailActivity.this,DetailForMovieActivity.class);
//					 Bundle bundle = new Bundle();
//					  bundle.putSerializable("data",movieInfos.get(position));
//					  intent.putExtras(bundle);
//					  startActivity(intent);
//				}
//			});
	    	   break;
	       default:
	           break;
	       }
	          super.handleMessage(msg);
	     }
	};
}
